package com.saudidentalapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.saudiapp.lazylist.ImageLoader;
import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Responce;
import com.saudiapp.webservice.Utilities;
import com.saudiapp.webservice.WebServiceManager;
import com.saudiapp.webservice.WebServiceManager.WebServiceListener;
import com.saudidentalapp.datamodels.GalleryItem;

public class GalleryActivity extends Activity implements WebServiceListener,OnItemClickListener
{
	
	Utilities utilities;
	GridView lstPosters;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallary);
		lstPosters=(GridView)findViewById(R.id.gridview);
		setView();
	}
	 
	private void setView()
	{
		utilities=Utilities.getInstance(GalleryActivity.this);
		utilities.showProgreessDialog();
		if(utilities.haveNetworkConnection())
		{
			WebServiceManager webServiceManager=new WebServiceManager(this);
			webServiceManager.setonWebServiceListener(this);
			webServiceManager.getGallaryInformation();
		}
		else
		{
		utilities.dismiss();
		utilities.showToast(Constants.INTERNET_NOT_AVAILABELE);
		}
		
		
		final Activity activity=this;
		ImageView imgBack=(ImageView)findViewById(R.id.xbtnVwBack);
		imgBack.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				finish();
				startActivity(new Intent(activity,TabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				
			}
		});
			
	}
	
	
	public class ImageAdapter extends BaseAdapter 
	{
	    private Context mContext;
	    ImageLoader imageLoader;
	    LayoutInflater inflater ;
	    ArrayList<GalleryItem> galleryItems=new ArrayList<GalleryItem>();
	    public ImageAdapter(Context c, ArrayList<GalleryItem> galleryItems) {
	        mContext = c;
	        this.galleryItems.addAll(galleryItems);
	        imageLoader=new ImageLoader(GalleryActivity.this);
	         inflater = ((Activity)mContext).getLayoutInflater();
	    }

	    public int getCount() {
	        return galleryItems.size();
	    }

	    public Object getItem(int position) {
	        return galleryItems.get(position);
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) 
	    {
	        ImageView imageView;
	        if (convertView == null) 
	        {  // if it's not recycled, initialize some attributes
	            imageView = new ImageView(mContext);
	            imageView.setBackgroundColor(Color.WHITE);
	            imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
	           imageView.setScaleType(ImageView.ScaleType.CENTER);
	           imageView.setPadding(4, 4, 4, 4);
	            
	        } 
	        else 
	        {
	            imageView = (ImageView) convertView;
	        }
	        GalleryItem galleryItem=(GalleryItem) getItem(position);
			imageLoader.DisplayImage(galleryItem.getThumbImage(), imageView);
			return imageView;
	       // imageView.setImageResource(mThumbIds[position]);
     
			
			/* ImageView imageView = null;
	    	if (convertView == null) 	        
	    		convertView = inflater.inflate(R.layout.gridview_image, parent, false);
	       	    	
	    		imageView=(ImageView) convertView.findViewById(R.id.imageViewGrid);    	

	    	GalleryItem galleryItem=(GalleryItem) getItem(position);
			imageLoader.DisplayImage(galleryItem.getThumbImage(), imageView);*/
	    	//return convertView;
	    }

	   
	}

	ArrayList<GalleryItem> galleryItems;
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) 
	{
		GalleryItem  galleryItem =galleryItems.get(position);
		
		if(galleryItem!=null)
			showZoomImageDialog(galleryItem);
		else
			utilities.showToast(Constants.ERROR_MESSAGE);
	}
	
	void showZoomImageDialog(GalleryItem  galleryItem)
	{
		FbDialog dialog=new FbDialog(GalleryActivity.this, galleryItem.getImage());
		
		WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();

		//WMLP.gravity = Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
		
		WMLP.height=(int) (getWindowManager().getDefaultDisplay().getHeight()/2);		
		dialog.getWindow().setAttributes(WMLP);	
		
		dialog.show();
	}

	@Override
	public void onProcesCompleted(int requestId, Object Data) 
	{
		utilities.dismiss();	
		galleryItems=((ArrayList<GalleryItem>)((Responce)Data).getResponceData());
		lstPosters.setAdapter(new ImageAdapter(this,galleryItems));
		lstPosters.setOnItemClickListener(this);
		
	}

	@Override
	public void onErrorOccured(int requestId)
	{
		utilities.dismiss();	
		utilities.showToast(Constants.ERROR_MESSAGE);
		
	}
}
